package com.neutech.code.view;

import com.neutech.code.dao.BusinessDAO;
import com.neutech.code.dao.impl.BusinessDAOImpl;
import com.neutech.code.entity.Business;
import java.util.Scanner;

import java.math.BigDecimal;
import java.util.List;

import static  com.neutech.code.constant.Constant.input;

public class BusinessView {
    private static BusinessDAO businessDAO=new BusinessDAOImpl();

   public static void showMenu(){
       System.out.println("1.查看所有商家信息");
       System.out.println("2.增加商家");
       System.out.println("3.修改商家");
       System.out.println("4.删除商家");
       System.out.println("5.查看所有食品信息");
       System.out.println("6.增加食品");
       System.out.println("7.修改食品");
       System.out.println("8.删除食品");
       System.out.println("9.回到上级菜单");
       System.out.println("请输入编号选择功能");
       int index= input.nextInt();
       if(index==1){
       showBusinesses();
       }else if(index==2){
           addBusiness();
       }else if(index==3){
           updateBusiness();
       }else if(index==4){
           deleteBusiness();
       }else if (index==9){
           AdminView.showMenu();
       }
   }
   public static  void showBusinesses(){
List<Business> businessList=businessDAO.selectAll();
if(businessList.size()==0){
    System.err.println("没有商家数据");
}else {
    for(Business business:businessList){
        System.out.println(business);
    }
    showMenu();
}
   }

        public static void addBusiness() {
            System.out.println("请输入商家ID:");
            int businessId = input.nextInt();
            input.nextLine();

            System.out.println("请输入密码:");
            String password = input.next();

            System.out.println("请输入商家名称:");
            String businessName = input.next();

            System.out.println("请输入商家地址:");
            String businessAddress = input.next();

            System.out.println("请输入商家介绍:");
            String businessExplain = input.next();

            System.out.println("请输入起送价:");
            BigDecimal startPrice = input.nextBigDecimal();

            System.out.println("请输入配送费:");
            BigDecimal deliveryPrice = input.nextBigDecimal();

            Business business = new Business();
            business.setBusinessId(businessId);
            business.setPassword(password);
            business.setBusinessName(businessName);
            business.setBusinessAddress(businessAddress);
            business.setBusinessExplain(businessExplain);
            business.setStartPrice(startPrice);
            business.setDeliveryPrice(deliveryPrice);
            try {
                businessDAO.addBusiness(business);
                System.out.println("商家数据添加成功！");
            } catch (Exception e) {
                System.err.println("添加商家数据时发生错误：" + e.getMessage());
            }
        }

        public static void updateBusiness() {
            System.out.println("请输入要更新的商家ID:");
            int businessId = input.nextInt();
            input.nextLine(); // 消耗掉换行符

            System.out.println("请输入新密码（直接回车保留原密码）:");
            String password = input.nextLine();
            if (password.isEmpty()) {
                // 用户没有输入新密码，可以设置为null或者获取原密码逻辑（这里简化为null）
                password = null;
            }

            System.out.println("请输入新商家名称（直接回车保留原名称）:");
            String businessName = input.nextLine();
            if (businessName.isEmpty()) {
                businessName = null;
            }


            String businessAddress = getUserInputWithDefault("请输入新商家地址（直接回车保留原地址）:", null);
            String businessExplain = getUserInputWithDefault("请输入新商家介绍（直接回车保留原介绍）:", null);
            BigDecimal startPrice = getUserBigDecimalInputWithDefault("请输入新起送价（直接回车保留原价）:", null);
            BigDecimal deliveryPrice = getUserBigDecimalInputWithDefault("请输入新配送费（直接回车保留原价）:", null);

            // 创建Business对象并设置新值
            Business business = new Business();
            business.setBusinessId(businessId);
            business.setPassword(password);
            business.setBusinessName(businessName);
            business.setBusinessAddress(businessAddress);
            business.setBusinessExplain(businessExplain);
            business.setStartPrice(startPrice);
            business.setDeliveryPrice(deliveryPrice);

            try {
                businessDAO.updateBusiness(business);
                System.out.println("商家数据更新成功！");
            } catch (Exception e) {
                System.err.println("更新商家数据时发生错误：" + e.getMessage());
            }
        }
private static String getUserInputWithDefault(String prompt, String defaultValue) {
    Scanner scanner = new Scanner(System.in);
    System.out.println(prompt);
    String input = scanner.nextLine();
    scanner.close();
    return input.isEmpty() ? defaultValue : input;
}

    // 辅助方法，用于获取BigDecimal类型的用户输入，如果直接回车则使用默认值
    private static BigDecimal getUserBigDecimalInputWithDefault(String prompt, BigDecimal defaultValue) {
        Scanner scanner = new Scanner(System.in);
        System.out.println(prompt);
        String input = scanner.nextLine();
        scanner.close();
        try {

            return input.isEmpty() ? defaultValue : new BigDecimal(input);
        } catch (NumberFormatException e) {

            System.err.println("输入的不是有效的数字格式，使用默认值。");
            return defaultValue;
        }
    }

//public static void deleteBusiness() {
//    try {
//        businessDAO.deleteBusiness(business);
//        System.out.println("商家数据删除成功！");
//    } catch (Exception e) {
//        System.err.println("删除商家数据时发生错误：" + e.getMessage());
//    }
//}
//public static void deleteBusiness(int businessId) {
//    System.out.println("请输入商家ID:");
//    businessId = input.nextInt();
//    input.nextLine();
//    try {
//        // 假设businessDAO是一个可以访问的DAO对象，可能是单例或者通过其他方式获取
//        businessDAO.deleteBusiness(businessId);
//        System.out.println("商家数据删除成功！");
//    } catch (Exception e) {
//        System.err.println("删除商家数据时发生错误：" + e.getMessage());
//    }
//}


        public static void deleteBusiness() {
            Scanner input = new Scanner(System.in);
            System.out.println("请输入商家ID:");
            int businessId = input.nextInt();
            input.nextLine(); // 消耗掉输入中的换行符

            try {
                businessDAO.deleteBusiness(businessId);
                
            } catch (RuntimeException e) { // 只捕获由deleteBusiness抛出的RuntimeException
                System.err.println("删除商家数据时发生错误：" + e.getMessage());
            }

            input.close(); // 不要忘记关闭Scanner对象
        }
    }






